import WxValidate from '../../utils/WxValidate.js';
import {HTTP} from "../../utils/http.js";
import Dialog from '../../miniprogram_npm/vant-weapp/dialog/dialog';
var http = new HTTP();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isFocus1:false,
    isFocus2:false,
    form:{
      phonenum:'',
      identicode:''
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initValidate();
  },
  showModal(error) {
    wx.showModal({
      content: error.msg,
      showCancel: false,
    })
  },
  initValidate() {
    const rules = {
      phonenum: {
        required: true,
        minlength:11
      },
      identicode:{
        required:true,
        minlength:6
      }
    }
    const messages = {
      phonenum: {
        required: '请输入手机号',
        minlength:'请输入正确的手机号'
      },
      identicode:{
        required:'请输入验证吗',
        minlength:'请输入6位正确的验证码'
      }
    }
    this.WxValidate = new WxValidate(rules, messages)
  }
 ,
  onShareAppMessage: function () {

  },
  phoneInput:function(e){
    this.setData({
      "form.phonenum": e.detail.value
    })
  },
  getPhoneSmsCode: function () {

    if (this.data.form.phonenum == "") {
      Dialog.alert({
        title: '提示',
        message: "请输入的手机号。"
      });
      return;
    }
    if (this.data.form.phonenum != "" && this.data.form.phonenum.length != 11) {
      Dialog.alert({
        title: '提示',
        message: "您输入的手机号有误。"
      });
      return;
    }
    let data = { "mobileNo": this.data.form.phonenum, "templateNo": "SMS_157682138" };
    http.request({
      url: "/sys/sendmsg",
      method: "POST",
      data: data,
      success: function (data) {
        wx.showToast({
          title: "已发送验证码。",
          icon: 'success',
          duration: 3000
        })
      }
    })
  },
  phoneChange:function(e){
      this.setData({
        "form.phonenum":e.detail.value
      })
  },
  codeChange:function(e){
    this.setData({
      "form.identicode": e.detail.value
    })
  },
  phoneTap:function(e){
      this.setData({
        isFocus1:true,
        isFocus2:false
      })
  },
  codeTap:function(){
    this.setData({
      isFocus1:false,
      isFocus2:true
    })
  },
  codeBlur:function(e){
      console.log("blur");
      this.setData({
        phoneNum:e.detail.value
      })
  },
  formSubmit:function(e){
    const params = e.detail.value;
    // 传入表单数据，调用验证方法
    if (!this.WxValidate.checkForm(params)) {
        const error = this.WxValidate.errorList[0]
        this.showModal(error)
        return false
    }
    let phonenum = params.phonenum;
    let identicode = params.identicode;
    this.verifyCode();
  },
  verifyCode:function(data){

    let that = this;
    http.request({
      url: "/sys/verifymsgcode",
      method: "POST",
      data: {
        "verifyCode":this.data.form.identicode
      },
      success: function (data) {
        if (data.errCode == "0011" || data.errCode == "0012") {
          Dialog.alert({
            title: '提示',
            message: data.errMsg
          });
          return;
        }else{
          that.login();
        }
      
      }
    })
  },
  login:function(){
    let updata = {
      "mobile": this.data.form.phonenum
    }
    http.request({
      url: "/usr/login",
      method: "POST",
      data:updata,
      success: function (data) {
        wx.switchTab({
          url: '/pages/index/index'
        });
      }
    })
  }
})